package project.suntray.com.core.net;

import java.io.File;
import java.util.List;
import java.util.WeakHashMap;

import commerce.zhangyang.com.core.net.callback.IError;
import commerce.zhangyang.com.core.net.callback.IFailure;
import commerce.zhangyang.com.core.net.callback.IRequest;
import commerce.zhangyang.com.core.net.callback.ISuccess;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by zhangyang on 2017/12/14 07:55.
 * version 1
 * RestClient 的构造器: 具体的变量的赋值放在这处理
 */
public class RestClientBuilder{
    private static final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();
    /**
     * 访问的url
     */
    private  String mUrl;

    /**
     * 文件对象
     */
    private  File mFile;

    /**
     * 文件集合
     */
    private List<File> fileList;

    /**
     * 下载的目录
     */
    private  String mDownloadDir;

    /**
     * 回调器
     */
    private  ISuccess mSuccess;
    private  IFailure mFailure;
    private  IError mError;
    private IRequest mRequest;
    private RequestBody mRequestBody;
    private String mExtention;


    /**
     * 设置 url
     * @param url
     * @return
     */
    public final RestClientBuilder url(String url){
        this.mUrl =url;
        return  this;
    }

    /**
     * 设置参数
     * @param params
     * @return
     */
    public final RestClientBuilder params(WeakHashMap<String,Object> params){
        this.PARAMS.putAll(params);
        return this;
    }

    /**
     * 设置参数
     * @param key
     * @param object
     * @return
     */
    public final RestClientBuilder params(String key, Object object) {
        this.PARAMS.put(key,object);
        return this;
    }

    /**
     * 设置下载目录
     * @param downloadDir
     * @return
     */
    public final RestClientBuilder downloadDir(String downloadDir) {
        this.mDownloadDir =downloadDir;
        return this;
    }

    /**
     * 设置文件
     * @param file
     * @return
     */
    public final RestClientBuilder file(File file) {
        this.mFile =file;
        return this;
    }

    /**
     * 放入文件集合
     * @param fileList
     * @return
     */
    public final RestClientBuilder fileList(List<File> fileList) {
        this.fileList=fileList;
        return this;
    }
    /**
     * 设置成功监听
     * @param iSuccess
     * @return
     */
    public final RestClientBuilder successListener(ISuccess iSuccess) {
        this.mSuccess =iSuccess;
        return this;
    }

    /**
     * 传递失败监听
     * @param iFailure
     * @return
     */
    public final RestClientBuilder failureListener(IFailure iFailure) {
        this.mFailure =iFailure;
        return this;
    }

    /**
     * 设置错误监听
     * @param iError
     * @return
     */
    public final RestClientBuilder errorListener(IError iError) {
        this.mError =iError;
        return this;
    }

    /**
     * 请求的监听器
     * @param iRequest
     * @return
     */
    public final RestClientBuilder requestListener(IRequest iRequest) {
        this.mRequest=iRequest;
        return this;
    }

    /**
     * 获取返回拓展字段
     * @param mExtention
     * @return
     */
    public final RestClientBuilder extention(String mExtention) {
        this.mExtention=mExtention;
        return this;
    }

    /**
     * 原始请求
     * @param raw
     * @return
     */
    public final RestClientBuilder raw(String raw){
        mRequestBody=RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),raw);
        return this;
    }


    /**
     * 返回构建的目标者!
     * @return
     */
    public RestClient build(){
        return  new RestClient(mUrl, mFile, fileList, mDownloadDir, mSuccess, mFailure, mError, mRequest, mRequestBody);
    }
}
